{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "8d70e8fb",
   "metadata": {},
   "outputs": [],
   "source": [
    "import sys\n",
    "import os\n",
    "import numpy as np\n",
    "import pynq\n",
    "import time"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "d17a0bc6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CLK_out0 is set to 480MHz\r\n"
     ]
    }
   ],
   "source": [
    "bitfile = \"deconv.bit\"\n",
    "overlay = pynq.Overlay(bitfile)\n",
    "!./dfs 0 480\n",
    "dma = overlay.axi_dma"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "e9f02b07",
   "metadata": {},
   "outputs": [],
   "source": [
    "input_height, input_width = 11, 21\n",
    "in_channels = 16\n",
    "out_channels = 1\n",
    "kernel_size = 3\n",
    "stride = 2\n",
    "padding = 1\n",
    "output_padding = 0\n",
    "out_height = (input_height - 1) * stride + kernel_size - 2 * padding + output_padding\n",
    "out_width = (input_width - 1) * stride + kernel_size - 2 * padding + output_padding\n",
    "\n",
    "P_ICH, P_OCH = 4, 1\n",
    "A_BIT, W_BIT, B_BIT = 8, 8, 32"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "c6c43419",
   "metadata": {},
   "outputs": [],
   "source": [
    "input_size = input_height * input_width * in_channels\n",
    "input_data = (np.arange(input_size) % 128).astype(np.uint8)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "0cc5148d",
   "metadata": {},
   "outputs": [],
   "source": [
    "input_buffer = pynq.allocate(shape=(in_channels * input_height * input_width), dtype=np.uint8, cacheable=1)\n",
    "output_buffer = pynq.allocate(shape=(out_channels * out_height * out_width), dtype=np.uint32, cacheable=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "af2634e6",
   "metadata": {},
   "outputs": [],
   "source": [
    "np.copyto(input_buffer, input_data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "392f5481",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "total_time  0.04079437255859375\n"
     ]
    }
   ],
   "source": [
    "start = time.time()\n",
    "for i in range(100):\n",
    "    dma.sendchannel.transfer(input_buffer)\n",
    "    dma.recvchannel.transfer(output_buffer)\n",
    "    dma.recvchannel.wait()\n",
    "    dma.sendchannel.wait()\n",
    "end = time.time()\n",
    "print(\"total_time \", end - start)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "fd0ec34b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "PynqBuffer([  8920,  32048,  27224,  68656,  45528, 105264,  63832,\n",
       "            141872,  82136, 178480, 100440, 215088, 118744, 251696,\n",
       "            137048, 174640,   8920,  32048,  27224,  68656,  45528,\n",
       "            105264,  63832, 141872,  82136, 178480, 100440, 215088,\n",
       "            118744, 251696, 137048, 174640,   8920,  32048,  27224,\n",
       "             68656,  45528, 105264,  63832, 141872,  82136,  47920,\n",
       "            108896,  84528, 149344, 121136, 174432, 109616, 133984,\n",
       "            146224, 174432, 182832, 214880, 219440, 255328, 256048,\n",
       "             83808,  47920, 108896,  84528, 149344, 121136, 174432,\n",
       "            109616, 133984, 146224, 174432, 182832, 214880, 219440,\n",
       "            255328, 256048,  83808,  47920, 108896,  84528, 149344,\n",
       "            121136, 174432, 109616, 133984, 146224, 100440, 215088,\n",
       "            118744, 251696, 137048, 174640,   8920,  32048,  27224,\n",
       "             68656,  45528, 105264,  63832, 141872,  82136, 178480,\n",
       "            100440, 215088, 118744, 251696, 137048, 174640,   8920,\n",
       "             32048,  27224,  68656,  45528, 105264,  63832, 141872,\n",
       "             82136, 178480, 100440, 215088, 118744, 251696, 137048,\n",
       "            174640,   8920,  32048,  27224, 182832, 214880, 219440,\n",
       "            255328, 256048,  83808,  47920, 108896,  84528, 149344,\n",
       "            121136, 174432, 109616, 133984, 146224, 174432, 182832,\n",
       "            214880, 219440, 255328, 256048,  83808,  47920, 108896,\n",
       "             84528, 149344, 121136, 174432, 109616, 133984, 146224,\n",
       "            174432, 182832, 214880, 219440, 255328, 256048,  83808,\n",
       "             47920, 108896,  84528,  45528, 105264,  63832, 141872,\n",
       "             82136, 178480, 100440, 215088, 118744, 251696, 137048,\n",
       "            174640,   8920,  32048,  27224,  68656,  45528, 105264,\n",
       "             63832, 141872,  82136, 178480, 100440, 215088, 118744,\n",
       "            251696, 137048, 174640,   8920,  32048,  27224,  68656,\n",
       "             45528, 105264,  63832, 141872,  82136, 178480, 100440,\n",
       "            215088, 118744, 121136, 174432, 109616, 133984, 146224,\n",
       "            174432, 182832, 214880, 219440, 255328, 256048,  83808,\n",
       "             47920, 108896,  84528, 149344, 121136, 174432, 109616,\n",
       "            133984, 146224, 174432, 182832, 214880, 219440, 255328,\n",
       "            256048,  83808,  47920, 108896,  84528, 149344, 121136,\n",
       "            174432, 109616, 133984, 146224, 174432, 182832, 214880,\n",
       "            219440, 137048, 174640,   8920,  32048,  27224,  68656,\n",
       "             45528, 105264,  63832, 141872,  82136, 178480, 100440,\n",
       "            215088, 118744, 251696, 137048, 174640,   8920,  32048,\n",
       "             27224,  68656,  45528, 105264,  63832, 141872,  82136,\n",
       "            178480, 100440, 215088, 118744, 251696, 137048, 174640,\n",
       "              8920,  32048,  27224,  68656,  45528, 105264,  63832,\n",
       "            256048,  83808,  47920, 108896,  84528, 149344, 121136,\n",
       "            174432, 109616, 133984, 146224, 174432, 182832, 214880,\n",
       "            219440, 255328, 256048,  83808,  47920, 108896,  84528,\n",
       "            149344, 121136, 174432, 109616, 133984, 146224, 174432,\n",
       "            182832, 214880, 219440, 255328, 256048,  83808,  47920,\n",
       "            108896,  84528, 149344, 121136, 174432, 109616,  82136,\n",
       "            178480, 100440, 215088, 118744, 251696, 137048, 174640,\n",
       "              8920,  32048,  27224,  68656,  45528, 105264,  63832,\n",
       "            141872,  82136, 178480, 100440, 215088, 118744, 251696,\n",
       "            137048, 174640,   8920,  32048,  27224,  68656,  45528,\n",
       "            105264,  63832, 141872,  82136, 178480, 100440, 215088,\n",
       "            118744, 251696, 137048, 174640,   8920, 146224, 174432,\n",
       "            182832, 214880, 219440, 255328, 256048,  83808,  47920,\n",
       "            108896,  84528, 149344, 121136, 174432, 109616, 133984,\n",
       "            146224, 174432, 182832, 214880, 219440, 255328, 256048,\n",
       "             83808,  47920, 108896,  84528, 149344, 121136, 174432,\n",
       "            109616, 133984, 146224, 174432, 182832, 214880, 219440,\n",
       "            255328, 256048,  83808,  47920,  27224,  68656,  45528,\n",
       "            105264,  63832, 141872,  82136, 178480, 100440, 215088,\n",
       "            118744, 251696, 137048, 174640,   8920,  32048,  27224,\n",
       "             68656,  45528, 105264,  63832, 141872,  82136, 178480,\n",
       "            100440, 215088, 118744, 251696, 137048, 174640,   8920,\n",
       "             32048,  27224,  68656,  45528, 105264,  63832, 141872,\n",
       "             82136, 178480, 100440,  84528, 149344, 121136, 174432,\n",
       "            109616, 133984, 146224, 174432, 182832, 214880, 219440,\n",
       "            255328, 256048,  83808,  47920, 108896,  84528, 149344,\n",
       "            121136, 174432, 109616, 133984, 146224, 174432, 182832,\n",
       "            214880, 219440, 255328, 256048,  83808,  47920, 108896,\n",
       "             84528, 149344, 121136, 174432, 109616, 133984, 146224,\n",
       "            174432, 182832, 118744, 251696, 137048, 174640,   8920,\n",
       "             32048,  27224,  68656,  45528, 105264,  63832, 141872,\n",
       "             82136, 178480, 100440, 215088, 118744, 251696, 137048,\n",
       "            174640,   8920,  32048,  27224,  68656,  45528, 105264,\n",
       "             63832, 141872,  82136, 178480, 100440, 215088, 118744,\n",
       "            251696, 137048, 174640,   8920,  32048,  27224,  68656,\n",
       "             45528, 219440, 255328, 256048,  83808,  47920, 108896,\n",
       "             84528, 149344, 121136, 174432, 109616, 133984, 146224,\n",
       "            174432, 182832, 214880, 219440, 255328, 256048,  83808,\n",
       "             47920, 108896,  84528, 149344, 121136, 174432, 109616,\n",
       "            133984, 146224, 174432, 182832, 214880, 219440, 255328,\n",
       "            256048,  83808,  47920, 108896,  84528, 149344, 121136,\n",
       "             63832, 141872,  82136, 178480, 100440, 215088, 118744,\n",
       "            251696, 137048, 174640,   8920,  32048,  27224,  68656,\n",
       "             45528, 105264,  63832, 141872,  82136, 178480, 100440,\n",
       "            215088, 118744, 251696, 137048, 174640,   8920,  32048,\n",
       "             27224,  68656,  45528, 105264,  63832, 141872,  82136,\n",
       "            178480, 100440, 215088, 118744, 251696, 137048, 109616,\n",
       "            133984, 146224, 174432, 182832, 214880, 219440, 255328,\n",
       "            256048,  83808,  47920, 108896,  84528, 149344, 121136,\n",
       "            174432, 109616, 133984, 146224, 174432, 182832, 214880,\n",
       "            219440, 255328, 256048,  83808,  47920, 108896,  84528,\n",
       "            149344, 121136, 174432, 109616, 133984, 146224, 174432,\n",
       "            182832, 214880, 219440, 255328, 256048,   8920,  32048,\n",
       "             27224,  68656,  45528, 105264,  63832, 141872,  82136,\n",
       "            178480, 100440, 215088, 118744, 251696, 137048, 174640,\n",
       "              8920,  32048,  27224,  68656,  45528, 105264,  63832,\n",
       "            141872,  82136, 178480, 100440, 215088, 118744, 251696,\n",
       "            137048, 174640,   8920,  32048,  27224,  68656,  45528,\n",
       "            105264,  63832, 141872,  82136,  47920, 108896,  84528,\n",
       "            149344, 121136, 174432, 109616, 133984, 146224, 174432,\n",
       "            182832, 214880, 219440, 255328, 256048,  83808,  47920,\n",
       "            108896,  84528, 149344, 121136, 174432, 109616, 133984,\n",
       "            146224, 174432, 182832, 214880, 219440, 255328, 256048,\n",
       "             83808,  47920, 108896,  84528, 149344, 121136, 174432,\n",
       "            109616, 133984, 146224, 100440, 215088, 118744, 251696,\n",
       "            137048, 174640,   8920,  32048,  27224,  68656,  45528,\n",
       "            105264,  63832, 141872,  82136, 178480, 100440, 215088,\n",
       "            118744, 251696, 137048, 174640,   8920,  32048,  27224,\n",
       "             68656,  45528, 105264,  63832, 141872,  82136, 178480,\n",
       "            100440, 215088, 118744, 251696, 137048, 174640,   8920,\n",
       "             32048,  27224, 182832, 214880, 219440, 255328, 256048,\n",
       "             83808,  47920, 108896,  84528, 149344, 121136, 174432,\n",
       "            109616, 133984, 146224, 174432, 182832, 214880, 219440,\n",
       "            255328, 256048,  83808,  47920, 108896,  84528, 149344,\n",
       "            121136, 174432, 109616, 133984, 146224, 174432, 182832,\n",
       "            214880, 219440, 255328, 256048,  83808,  47920, 108896,\n",
       "             84528,  45528, 105264,  63832, 141872,  82136, 178480,\n",
       "            100440, 215088, 118744, 251696, 137048, 174640,   8920,\n",
       "             32048,  27224,  68656,  45528, 105264,  63832, 141872,\n",
       "             82136, 178480, 100440, 215088, 118744, 251696, 137048,\n",
       "            174640,   8920,  32048,  27224,  68656,  45528, 105264,\n",
       "             63832, 141872,  82136, 178480, 100440, 215088, 118744],\n",
       "           dtype=uint32)"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "output_buffer"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d97a38bd",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
